<?php
/**
 * User: krisbuist
 * Date: 1/20/12
 * Time: 3:04 PM
 */


require_once APPPATH . 'core/controller.php';

class NewsCrawler extends Controller
{
    private $fetchAtOnce = 50;

    public function index($begin)
    {
        $from = $begin;
        $to = $begin + $this->fetchAtOnce;

        echo sprintf('<a href="%s">Crawl berichten %s-%s</a>', $this->getCrawlUrl($from, $to), $from, $to);
    }

    public function crawl($from, $to)
    {
        for ($i = $from; $i < $to; $i++)
        {
            $url = $this->getTweakersURL($i);
            $content = file_get_contents($url);
            $contentPattern = '/<div class=\"article\">(.*)<\/div>/iU';

            preg_match($contentPattern, $content, $contentMatches);


            $titlePattern = '/<h1>(.*)<\/h1>/iU';
            preg_match($titlePattern, $content, $titleMatches);


            if (isset($contentMatches[1], $titleMatches[1])) {

                $articleContent = $contentMatches[1];
                $articleTitle = $titleMatches[1];
                try {
                    $post = new Post();
                    $user = $this->em->find('User', rand(1, 5));
                    $post->setAuthor($user);

                    $postContent = new Post_Content();
                    $postContent->setContent($articleContent);

                    $post->setContent($postContent);

                    $category = $this->em->find('Category', rand(1, 5));

                    $post->setCategory($category);

                    $post->setTitle($articleTitle);

                    $this->em->persist($post);
                }
                catch (PDOException $e)
                {
                    echo 'PDOException, volgende....<br/>';
                }
                catch (\Doctrine\ORM\ORMException $e)
                {
                    echo 'ORMException, volgende....<br/>' . $e->getMessage();
                }
            }

        }
        $this->em->flush();
        //        try {
        //            $this->em->flush();
        //        }
        //        catch (PDOException $e)
        //        {
        //            echo 'Jajaja, weer zo een PDOException: <br/>' . $e->getMessage() . '<br />';
        //        }

        $from = $to;
        $to = $to + $this->fetchAtOnce;

        //echo sprintf('<a href="%s">Crawl berichten %s-%s</a>', $this->getCrawlUrl($from, $to), $from, $to);
        echo sprintf('<meta http-equiv="refresh" content="0;url=%s">', $this->getCrawlUrl($from, $to));
    }

    private function getCrawlUrl($from, $to)
    {
        return sprintf('/crawl/crawl/%s/%s', $from, $to);
    }

    private function getTweakersURL($id)
    {
        return sprintf('https://api.tweakers.net/news/%d', $id);
    }


}
